home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htslib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-25  |  17.1 KB  |  475 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Subroutines .h                                         */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier librairie .h
  38.  
  39. #ifndef HTS_DEFH
  40. #define HTS_DEFH 
  41.  
  42. /* dΘfinitions globales */
  43. #include "htsglobal.h"
  44.  
  45. /* basic net definitions */
  46. #include "htsbase.h"
  47. #include "htsbasenet.h"
  48. #include "htsnet.h"
  49.  
  50. /* cookies et auth */
  51. #include "htsbauth.h"
  52.  
  53. // Attention, dΘfinition existante Θgalement dans le shell
  54. // (α modifier avec celle-ci)
  55. #define POSTTOK "?>post"
  56.  
  57. #include "htsopt.h"
  58.  
  59. // structure pour paramΦtres supplΘmentaires lors de la requΩte
  60. typedef struct {
  61.   short int user_agent_send;  // user agent (ex: httrack/1.0 [sun])
  62.   short int http11;           // l'en tΩte peut (doit) Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  63.   short int nokeepalive;      // pas de keep-alive
  64.   short int range_used;       // Range utilisΘ
  65.   short int nocompression;    // Pas de compression
  66.   short int flush_garbage;    // recycled
  67.   char user_agent[128];
  68.   char referer[256];
  69.   char from[256];
  70.   char lang_iso[64];
  71.   t_proxy proxy;              // proxy
  72. } htsrequest;
  73.  
  74.  
  75. // structure pour retour d'une connexion/prise d'en tΩte
  76. typedef struct {
  77.   int statuscode;        // status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  78.   short int notmodified; // page ou fichier NON modifiΘ (transfΘrΘ)
  79.   short int is_write;    // sortie sur disque (out) ou en mΘmoire (adr)
  80.   short int is_chunk;    // mode chunk
  81.   short int compressed;  // compressΘ?
  82.   short int empty;       // vide?
  83.   short int keep_alive;  // Keep-Alive?
  84.   short int keep_alive_trailers;  // ..with trailers extension
  85.   int keep_alive_t;      // KA timeout
  86.   int keep_alive_max;    // KA number of requests
  87.   char* adr;             // adresse du bloc de mΘmoire, NULL=vide
  88.   char* headers;         // adresse des en tΩtes si prΘsents
  89.   FILE* out;             // Θcriture directe sur disque (si is_write=1)
  90.   LLint size;            // taille fichier
  91.   char msg[80];          // message Θventuel si Θchec ("\0"=non prΘcisΘ)
  92.   char contenttype[64];  // content-type ("text/html" par exemple)
  93.   char charset[64];      // charset ("iso-8859-1" par exemple)
  94.   char contentencoding[64];  // content-encoding ("gzip" par exemple)
  95.   char* location;        // on copie dedans Θventuellement la vΘritable 'location'
  96.   LLint totalsize;       // taille totale α tΘlΘcharger (-1=inconnue)
  97.   short int is_file;     // ce n'est pas une socket mais un descripteur de fichier si 1
  98.   T_SOC soc;             // ID socket
  99.   SOCaddr address;       // IP address
  100.   int     address_size;  // IP address structure length
  101.   FILE* fp;              // fichier pour file://
  102. #if HTS_USEOPENSSL
  103.   short int ssl;         // is this connection a SSL one? (https)
  104.   // BIO* ssl_soc;          // SSL structure
  105.   SSL * ssl_con;         // connection structure
  106. #endif
  107.   char lastmodified[64]; // Last-Modified
  108.   char etag[64];         // Etag
  109.   char cdispo[256];      // Content-Disposition coupΘ
  110.   LLint  crange;         // Content-Range
  111.   int debugid;           // debug connection
  112.   /* */
  113.   htsrequest req;        // paramΦtres pour la requΩte
  114.   /*char digest[32+2];   // digest md5 gΘnΘrΘ par le moteur ("" si non gΘnΘrΘ)*/
  115. } htsblk;
  116.  
  117.  
  118. /* ANCIENNE STURCTURE pour cache 1.0 */
  119. typedef struct {
  120.   int statuscode;  // ANCIENNE STURCTURE - status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  121.   int notmodified; // ANCIENNE STURCTURE - page ou fichier NON modifiΘ (transfΘrΘ)
  122.   int is_write;    // ANCIENNE STURCTURE - sortie sur disque (out) ou en mΘmoire (adr)
  123.   char* adr;       // ANCIENNE STURCTURE - adresse du bloc de mΘmoire, NULL=vide
  124.   FILE* out;       // ANCIENNE STURCTURE - Θcriture directe sur disque (si is_write=1)
  125.   int size;        // ANCIENNE STURCTURE - taille fichier
  126.   char msg[80];    // ANCIENNE STURCTURE - message Θventuel si Θchec ("\0"=non prΘcisΘ)
  127.   char contenttype[64];  // ANCIENNE STURCTURE - content-type ("text/html" par exemple)
  128.   char* location;  // ANCIENNE STURCTURE - on copie dedans Θventuellement la vΘritable 'location'
  129.   int totalsize;   // ANCIENNE STURCTURE - taille totale α tΘlΘcharger (-1=inconnue)
  130.   int is_file;     // ANCIENNE STURCTURE - ce n'est pas une socket mais un descripteur de fichier si 1
  131.   T_SOC soc;       // ANCIENNE STURCTURE - ID socket
  132.   FILE* fp;        // ANCIENNE STURCTURE - fichier pour file://
  133.   t_proxy proxy;   // ANCIENNE STURCTURE - proxy
  134.   int user_agent_send;  // ANCIENNE STURCTURE - user agent (ex: httrack/1.0 [sun])
  135.   char user_agent[64];
  136.   int http11;           // ANCIENNE STURCTURE - l'en tΩte doit Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  137. } OLD_htsblk;
  138. /* fin ANCIENNE STURCTURE pour cache 1.0 */
  139.  
  140. // cache pour le dns, pour Θviter de faire des gethostbyname sans arrΩt
  141. typedef struct t_dnscache {
  142.   char iadr[1024];
  143.   struct t_dnscache* n;
  144.   char host_addr[HTS_MAXADDRLEN];    // 4 octets (v4), ou 16 octets (v6)
  145.   int host_length;                   // 4 normalement - ==0  alors en cours de rΘsolution
  146.                                      // ou >16 si sockaddr
  147.                                      //                 ==-1 alors erreur (host n'Θxiste pas)
  148. } t_dnscache;
  149.  
  150.  
  151.  
  152. /* Library internal definictions */
  153. #ifdef HTS_INTERNAL_BYTECODE
  154.  
  155. // fonctions unix/winsock
  156. int hts_read(htsblk* r,char* buff,int size);
  157. //int HTS_TOTAL_RECV_CHECK(int var);
  158. LLint check_downloadable_bytes(int rate);
  159.  
  160. #ifndef HTTRACK_DEFLIB
  161. HTSEXT_API int hts_init(void);
  162. HTSEXT_API int hts_uninit(void);
  163. #endif
  164.  
  165. // fonctions principales
  166. int http_fopen(char* adr,char* fil,htsblk* retour);
  167. int http_xfopen(int mode,int treat,int waitconnect,char* xsend,char* adr,char* fil,htsblk* retour);
  168. int http_sendhead(t_cookie* cookie,int mode,char* xsend,char* adr,char* fil,char* referer_adr,char* referer_fil,htsblk* retour);
  169. htsblk httpget(char* url);
  170. //int newhttp(char* iadr,char* err=NULL);
  171. int newhttp(char* iadr,htsblk* retour,int port,int waitconnect);
  172. HTS_INLINE void deletehttp(htsblk* r);
  173. HTS_INLINE int  deleteaddr(htsblk* r);
  174. HTS_INLINE void deletesoc(T_SOC soc);
  175. HTS_INLINE void deletesoc_r(htsblk* r);
  176. htsblk http_location(char* adr,char* fil,char* loc);
  177. htsblk http_test(char* adr,char* fil,char* loc);
  178. int check_readinput(htsblk* r);
  179. int check_readinput_t(T_SOC soc, int timeout);
  180. void http_fread(T_SOC soc,htsblk* retour);
  181. LLint http_fread1(htsblk* r);
  182. void treathead(t_cookie* cookie,char* adr,char* fil,htsblk* retour,char* rcvd);
  183. void treatfirstline(htsblk* retour,char* rcvd);
  184. #ifndef HTTRACK_DEFLIB
  185. HTSEXT_API void infostatuscode(char* msg,int statuscode);
  186. #endif
  187.  
  188. // sous-fonctions
  189. htsblk xhttpget(char* adr,char* fil);
  190. htsblk http_gethead(char* adr,char* fil);
  191. LLint http_xfread1(htsblk* r,int bufl);
  192. HTS_INLINE t_hostent* hts_gethostbyname(char* iadr, void* v_buffer);
  193. #ifndef HTTRACK_DEFLIB
  194. HTSEXT_API t_hostent* vxgethostbyname(char* hostname, void* v_buffer);
  195. #endif
  196. t_hostent* _hts_ghbn(t_dnscache* cache,char* iadr,t_hostent* retour);
  197. int ftp_available(void);
  198. #if HTS_DNSCACHE
  199. void hts_cache_free(t_dnscache* cache);
  200. int hts_dnstest(char* _iadr);
  201. t_dnscache* _hts_cache(void);
  202. int _hts_lockdns(int i);
  203. #endif
  204.  
  205. // outils divers
  206. HTS_INLINE TStamp time_local(void);
  207. #ifndef HTTRACK_DEFLIB
  208. HTSEXT_API HTS_INLINE TStamp mtime_local(void);
  209. #endif
  210. void sec2str(char *s,TStamp t);
  211. #ifndef HTTRACK_DEFLIB
  212. HTSEXT_API void qsec2str(char *st,TStamp t);
  213. #endif
  214. void time_gmt_rfc822(char* s);
  215. void time_local_rfc822(char* s);
  216. struct tm* convert_time_rfc822(char* s);
  217. int set_filetime(char* file,struct tm* tm_time);
  218. int set_filetime_rfc822(char* file,char* date);
  219. int get_filetime_rfc822(char* file,char* date);
  220. HTS_INLINE void time_rfc822(char* s,struct tm * A);
  221. HTS_INLINE void time_rfc822_local(char* s,struct tm * A);
  222. #ifndef HTTRACK_DEFLIB
  223. HTSEXT_API char* int2char(int n);
  224. HTSEXT_API char* int2bytes(LLint n);
  225. HTSEXT_API char* int2bytessec(long int n);
  226. HTSEXT_API char** int2bytes2(LLint n);
  227. #endif
  228. HTS_INLINE int sendc(htsblk* r, char* s);
  229. int finput(int fd,char* s,int max);
  230. int binput(char* buff,char* s,int max);
  231. int linput(FILE* fp,char* s,int max);
  232. int linputsoc(T_SOC soc, char* s, int max);
  233. int linputsoc_t(T_SOC soc, char* s, int max, int timeout);
  234. int linput_trim(FILE* fp,char* s,int max);
  235. int linput_cpp(FILE* fp,char* s,int max);
  236. void rawlinput(FILE* fp,char* s,int max);
  237. char* strstrcase(char *s,char *o);
  238. int ident_url_absolute(char* url,char* adr,char* fil);
  239. void fil_simplifie(char* f);
  240. int is_unicode_utf8(unsigned char* buffer, unsigned int size);
  241. void map_characters(unsigned char* buffer, unsigned int size, unsigned int* map);
  242. int ishtml(const char* urlfil);
  243. int ishtml_ext(const char* a);
  244. int ishttperror(int err);
  245. void guess_httptype(char *s,const char *fil);
  246. void get_httptype(char *s,const char *fil,int flag);
  247. int get_userhttptype(int setdefs,char *s,const char *ext);
  248. void give_mimext(char *s,char *st);
  249. int is_knowntype(const char *fil);
  250. int is_userknowntype(const char *fil);
  251. int is_dyntype(const char *fil);
  252. char* get_ext(const char *fil);
  253. int may_unknown(const char* st);
  254. #ifndef HTTRACK_DEFLIB
  255. HTSEXT_API char* jump_identification(char*);
  256. HTSEXT_API char* jump_normalized(char*);
  257. HTSEXT_API char* jump_toport(char*);
  258. HTSEXT_API char* fil_normalized(char* source, char* dest);
  259. HTSEXT_API char* adr_normalized(char* source, char* dest);
  260. #endif
  261. char* strrchr_limit(char* s, char c, char* limit);
  262. char* strstr_limit(char* s, char* sub, char* limit);
  263. HTS_INLINE char* jump_protocol(char* source);
  264. void code64(unsigned char* a,int size_a,unsigned char* b,int crlf);
  265. #ifndef HTTRACK_DEFLIB
  266. HTSEXT_API void unescape_amp(char* s);
  267. HTSEXT_API void escape_spc_url(char* s);
  268. HTSEXT_API void escape_in_url(char* s);
  269. HTSEXT_API void escape_uri(char* s);
  270. HTSEXT_API void escape_uri_utf(char* s);
  271. HTSEXT_API void escape_check_url(char* s);
  272. HTSEXT_API char* escape_check_url_addr(char* s);
  273. HTSEXT_API void x_escape_http(char* s,int mode);
  274. HTSEXT_API void escape_remove_control(char* s);
  275. #endif
  276. int ehexh(char c);
  277. #ifndef HTTRACK_DEFLIB
  278. HTSEXT_API char* unescape_http(char* s);
  279. HTSEXT_API char* unescape_http_unharm(char* s, int no_high);
  280. HTSEXT_API char* antislash_unescaped(char* s);
  281. #endif
  282. int ehex(char* s);
  283. char* concat(const char* a,const char* b);
  284. #define copychar(a) concat((a),NULL)
  285. #if HTS_DOSNAME
  286. char* fconcat(char* a,char* b);
  287. char* fconv(char* a);
  288. #else
  289. #define fconv(a) (a)
  290. #define fconcat(a,b) concat(a,b)
  291. #endif
  292. char* fslash(char* a);
  293. char* __fslash(char* a);
  294.  
  295. char* convtolower(char* a);
  296. char* concat(const char* a,const char* b);
  297. void hts_lowcase(char* s);
  298. void hts_replace(char *s,char from,char to);
  299.  
  300.  
  301. void fprintfio(FILE* fp,char* buff,char* prefix);
  302.  
  303. #if HTS_WIN
  304. #else
  305. int sig_ignore_flag( int setflag );     // flag ignore
  306. #endif
  307.  
  308. void cut_path(char* fullpath,char* path,char* pname);
  309. int fexist(char* s);
  310. /*LLint fsize(char* s);    */
  311. INTsys fpsize(FILE* fp);
  312. INTsys fsize(char* s);    
  313. /* root dir */
  314. #ifndef HTTRACK_DEFLIB
  315. HTSEXT_API char* hts_rootdir(char* file);
  316. #endif
  317.  
  318. // Threads
  319. #if USE_PTHREAD
  320. typedef void* ( *beginthread_type )( void * );
  321. unsigned long _beginthread( beginthread_type start_address, unsigned stack_size, void *arglist );
  322. #endif
  323.  
  324.  
  325.  
  326.  
  327. /* variables globales */
  328. //extern LLint HTS_TOTAL_RECV;  // flux entrant reτu
  329. //extern int HTS_TOTAL_RECV_STATE;  // status: 0 tout va bien 1: ralentir un peu 2: ralentir 3: beaucoup
  330. extern HTSEXT_API hts_stat_struct HTS_STAT;
  331. extern int _DEBUG_HEAD;
  332. extern FILE* ioinfo;
  333.  
  334. /* constantes */
  335. extern const char* hts_mime_keep[];
  336. extern const char* hts_mime[][2];
  337. extern const char* hts_main_mime[];
  338. extern const char* hts_detect[];
  339. extern const char* hts_detectbeg[];
  340. extern const char* hts_nodetect[];
  341. extern const char* hts_detectURL[];
  342. extern const char* hts_detectandleave[];
  343. extern const char* hts_detect_js[];
  344.  
  345. // defaut wrappers
  346. void  __cdecl htsdefault_init(void);
  347. void  __cdecl htsdefault_uninit(void);
  348. int   __cdecl htsdefault_start(void* opt);
  349. int   __cdecl htsdefault_chopt(void* opt);
  350. int   __cdecl htsdefault_end(void);
  351. int   __cdecl htsdefault_checkhtml(char* html,int len,char* url_adresse,char* url_fichier);
  352. int   __cdecl htsdefault_loop(void* back,int back_max,int back_index,int lien_n,int lien_tot,int stat_time,hts_stat_struct* stats);
  353. char* __cdecl htsdefault_query(char* question);
  354. char* __cdecl htsdefault_query2(char* question);
  355. char* __cdecl htsdefault_query3(char* question);
  356. int   __cdecl htsdefault_check(char* adr,char* fil,int status);
  357. void  __cdecl htsdefault_pause(char* lockfile);
  358. void  __cdecl htsdefault_filesave(char*);
  359. int   __cdecl htsdefault_linkdetected(char* link);
  360. int   __cdecl htsdefault_xfrstatus(void* back);
  361. int   __cdecl htsdefault_savename(char* adr_complete,char* fil_complete,char* referer_adr,char* referer_fil,char* save);
  362. int   __cdecl htsdefault_sendheader(char* buff, char* adr, char* fil, char* referer_adr, char* referer_fil, htsblk* outgoing);
  363. int   __cdecl htsdefault_receiveheader(char* buff, char* adr, char* fil, char* referer_adr, char* referer_fil, htsblk* incoming);
  364.  
  365. // end defaut wrappers
  366.  
  367.  
  368. // htsmodule.c definitions
  369. extern void* getFunctionPtr(httrackp* opt, char* file, char* fncname);
  370. extern void clearCallbacks(htscallbacks* chain);
  371.  
  372.  
  373.  
  374. #endif    // internals
  375.  
  376.  
  377. /* Spaces: CR,LF,TAB,FF */
  378. #define  is_space(c)      ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) || ((c)=='\'') )
  379. #define  is_realspace(c)  ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11)                )
  380. #define  is_taborspace(c) ( ((c)==' ')                                          || ((c)==9)                             )
  381. #define  is_quote(c)      (               ((c)=='\"')                                                    || ((c)=='\'') )
  382. #define  is_retorsep(c)   (                              ((c)==10) || ((c)==13) || ((c)==9)                                          )
  383. //HTS_INLINE int is_space(char);
  384. //HTS_INLINE int is_realspace(char);
  385.  
  386. // compare le dΘbut de f avec s et retourne la position de la fin
  387. // 'A=a' (case insensitive)
  388. static int strfield(const char* f,const char* s) {
  389.   int r=0;
  390.   while (streql(*f,*s) && ((*f)!=0) && ((*s)!=0)) { f++; s++; r++; }
  391.   if (*s==0)
  392.     return r;
  393.   else
  394.     return 0;
  395. }
  396. static int strcmpnocase(char* a,char* b) {
  397.   while(*a) {
  398.     int cmp = hichar(*a) - hichar(*b);
  399.     if (cmp != 0)
  400.       return cmp;
  401.     a++;
  402.     b++;
  403.   }
  404.   return 0;
  405. }
  406.  
  407. #define strfield2(f,s) ( (strlen(f)!=strlen(s)) ? 0 : (strfield(f,s)) )
  408.  
  409. // is this MIME an hypertext MIME (text/html), html/js-style or other script/text type?
  410. #define HTS_HYPERTEXT_DEFAULT_MIME "text/html"
  411. #define is_hypertext_mime__(a) \
  412.   ( (strfield2((a),"text/html")!=0)\
  413.   || (strfield2((a),"application/x-javascript")!=0) \
  414.   || (strfield2((a),"text/css")!=0) \
  415.   /*|| (strfield2((a),"text/vnd.wap.wml")!=0)*/ \
  416.   || (strfield2((a),"image/svg+xml")!=0) \
  417.   || (strfield2((a),"image/svg-xml")!=0) \
  418.   /*|| (strfield2((a),"audio/x-pn-realaudio")!=0) */\
  419.   || (strfield2((a),"application/x-authorware-map")!=0) \
  420.   )
  421. #define may_be_hypertext_mime__(a) \
  422.    (\
  423.      (strfield2((a),"audio/x-pn-realaudio")!=0) \
  424.      || (strfield2((a),"audio/x-mpegurl")!=0) \
  425.   )
  426.  
  427.  
  428. /* Library internal definictions */
  429. #ifdef HTS_INTERNAL_BYTECODE
  430.  
  431. // check if (mime, file) is hypertext
  432. static int is_hypertext_mime(const char* mime, const char* file) {
  433.   if (is_hypertext_mime__(mime))
  434.     return 1;
  435.   if (may_unknown(mime)) {
  436.     char guessed[256];
  437.     guessed[0] = '\0';
  438.     guess_httptype(guessed, file);
  439.     return is_hypertext_mime__(guessed);
  440.   }
  441.   return 0;
  442. }
  443.  
  444. // check if (mime, file) might be "false" hypertext
  445. static int may_be_hypertext_mime(const char* mime, const char* file) {
  446.   if (may_be_hypertext_mime__(mime))
  447.     return 1;
  448.   if (file != NULL && file[0] != '\0' && may_unknown(mime)) {
  449.     char guessed[256];
  450.     guessed[0] = '\0';
  451.     guess_httptype(guessed, file);
  452.     return may_be_hypertext_mime__(guessed);
  453.   }
  454.   return 0;
  455. }
  456.  
  457. // compare (mime, file) with reference
  458. static int compare_mime(const char* mime, const char* file, const char* reference) {
  459.   if (is_hypertext_mime__(mime) || may_be_hypertext_mime__(mime))
  460.     return strfield2(mime, reference);
  461.   if (file != NULL && file[0] != '\0' && may_unknown(mime)) {
  462.     char guessed[256];
  463.     guessed[0] = '\0';
  464.     guess_httptype(guessed, file);
  465.     return strfield2(guessed, reference);
  466.   }
  467.   return 0;
  468. }
  469.  
  470. #endif
  471.  
  472. #endif
  473.  
  474.  
  475.